package com.aigou.controller;

import com.aigou.pojo.Goods;
import com.aigou.pojo.PageResult;
import com.aigou.pojo.Result;
import com.aigou.pojo.SkillGoods;
import com.aigou.pojo.User;
import com.aigou.service.GoodsService;
import com.aigou.service.ISkillGoodsService;
import com.aigou.service.UserService;
import com.github.pagehelper.PageInfo;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    ISkillGoodsService skillGoodsService;
    
    @PostMapping(value = "/skillGoodsSet")
    @ResponseBody
    public Result skillGoodsSet(@RequestBody SkillGoods skillGoods) {
    	
    	// 查询商品
    	Goods goods = goodsService.findOne(skillGoods.getGid());
		// 插入秒杀表
    	skillGoods.setGname(goods.getName());
    	// skillGoods.setGurl(goods.getImage());
    	skillGoods.setPrice(goods.getSellPrice());
    	skillGoodsService.save(skillGoods );
    	
    	// 扣减库存
    	goodsService.updateNum(skillGoods.getGid(), goods.getNum() - skillGoods.getSkillNum(), goods.getVersion());
    	
    	Result result = new Result();
    	return result;
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ResponseBody
    public Result findAll(@RequestParam(value = "page", defaultValue = "1") Integer page
            ,@RequestParam(value = "size", defaultValue = "5")  Integer size, Goods goods) {

        Result result = new Result();
        PageInfo<Goods> pageInfo = goodsService.findAll(page, size, goods);
        PageResult pr = new PageResult();
        pr.setRows(pageInfo.getTotal());
        pr.setList(pageInfo.getList());
        result.setData(pr);
        return result;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody Goods goods) {
        Result result = new Result();
        goodsService.add(goods);
        return result;
    }

    @RequestMapping(value = "/addKuCun", method = RequestMethod.GET)
    @ResponseBody
    public Result addKuCun(Integer gid, int num) {
        Result result = new Result();
        goodsService.addKuCun(gid, num);
        return result;
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestBody Goods goods) {
        Result result = new Result();
        goodsService.update(goods);
        return result;
    }

    /**
     * 审核
     */
    @RequestMapping(value = "/check/{id}/{status}", method = RequestMethod.GET)
    @ResponseBody
    public Result check(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        Result result = new Result();
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(status);
        goodsService.update(goods);
        return result;
    }

    @RequestMapping(value = "/delete/{uid}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result del(@PathVariable("uid") Integer uid) {
        Result result = new Result();
        goodsService.delete(uid);
        return result;
    }

    @RequestMapping(value = "/findOne/{uid}", method = RequestMethod.GET)
    @ResponseBody
    public Result findOne(@PathVariable("uid") Integer id) {
        Result result = new Result();
        result.setData(goodsService.findOne(id));
        return result;
    }

}
